<?php
require_once 'BaseController.php';
class Admin_AdminController extends BaseController
{
    public function init(){
          Zend_Loader::loadClass('AuthModel');
          Zend_Loader::loadClass('AdminModel');
    }
    
    public function accountAction(){
		$dt = new AdminModel();
		$requestnew = $this->_request;
		$request = $this->getRequest();
        
		if ($this->_request->isPost()){
			$id = $request->getParam('id');
		//	echo $id." id";
			$name = $request->getParam('name');
			$email = $request->getParam('email');
			$role = $request->getParam('role');
		//	echo $name." name  ".$email."  mail  ".$address."  dia chi";
		//	Zend_debug::dump($email);
            if($name != "" && $email != ""){
				if((trim($name) != "") && (trim($email) != "")){
					 $validator = new Zend_Validate_EmailAddress();
					 $email = trim($email);
					
					 if ($validator->isValid($email)){
						   $data = array("name" => $name,
						                 "sunnet_email" => $email,
						                 "role" => $role,
                                         "status"=> '1'
                                         );
						   if($requestnew->getParam('submit') == 'ADD'){
						   		$test = $dt->get_email_admin($email);
						   		$count_test = count($test);
						   		if($count_test == 0) $dt->insert($data);
						    	else echo "<br><br><p align= 'center' style='color:red;'>Email bạnn nhập vào dã có, xin vui lòng kiêm tra lại. </p>";
					       }
					       else if($requestnew->getParam('submit') == 'EDIT'){
    	 	  	                 $dt->update($data,"id=".$id);
                   	/*			 $test = $dt->get_email_admin($email);
						   		 $count_test = count($test);
                                 Zend_Debug::dump($count_test);die;
						   		 if($count_test == 0) $dt->update($data,"id=".$id);
				    	         else echo "<br><br><p align= 'center' style='color:red;'>Email b?n nh?p vào dã có, xin vui lòng ki?m tra l?i. </p>";
					    */   }
					 }else echo "<br><br><p align= 'center' style='color:red;'>Ðịnh dạng email không hợp lệ </p>";
				}
				else echo "<br><br><p align= 'center' style='color:red;'>Ô Email hoặc ô Name là không trống. Xin vui lòng kiểm tra lại</p>";
			}
		}
		$this->view->datas = $dt->admin();
		
	}
	
	public function deleteacountAction(){
		$dt = new AdminModel();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		if($id){
			$dt->delete("id=".$id);
		}
		exit();
	}
	
    public function editacountAction(){
    	$layout = $this->_helper->layout();
		$layout->disableLayout();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		$dt = new AdminModel();
		$this->view->data = $dt->id_admin($id);
	}
	 
	public function editstatusAction(){
		$layout = $this->_helper->layout();
		$layout->disableLayout();
		$dt = new AdminModel();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		$status = $request->getParam('status');
		if($id){
			if($status == 1) $status=0;
			else if($status ==0) $status=1;
			$data = array("status" => $status);
			$dt->update($data,"id=".$id);
			$this->view->data = $dt->id_admin($id);
		}
		
	}
	
	
}